package com.shujia.sql

import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.{DataFrame, SparkSession, UDFRegistration, functions}

object SparkSQLUDFDemo {
  def main(args: Array[String]): Unit = {
    val ss: SparkSession = SparkSession.builder()
      .master("local")
      .appName("spark sql自定义函数")
      .config("spark.sql.shuffle.partitions", "1")
      .getOrCreate()


    import ss.implicits._
    import org.apache.spark.sql.functions._

    val studentDF: DataFrame = ss.read
      .format("csv")
      .option("sep", ",")
      .schema("id STRING,name STRING,age INT,gender STRING,clazz STRING")
      .load("spark/data/students.txt")

    //自定义udf函数
    val fz: UserDefinedFunction = functions.udf((s: String) => "数加：" + s)

    studentDF.select($"id", $"name", fz($"name") as "new_name")
      .show()


  }
}
